<?php

@include_once("connect.php");

$res = [];
$orderName = $_GET['orderName'];
$type = $_GET['type'];
$num = $_GET["num"];
$page = $_GET["page"];
$kw = $_GET['kw'];

if ($num && $page) {
  if ($type == 1) {
    $type = 'asc';
  } else {
    $type = 'desc';
  }
  $sql = "SELECT COUNT(*) total FROM goods_list WHERE goods_title LIKE '%$kw%'";
  $a = mysqli_query($con, $sql);
  $arr = mysqli_fetch_array($a);
  $total = $arr["total"];

  $start = ($page - 1) * $num;
  $sql = "SELECT * FROM goods_list WHERE goods_title LIKE '%$kw%' ORDER BY $orderName $type LIMIT $start,$num";
  $a = mysqli_query($con, $sql);
  $list = [];
  while ($arr = mysqli_fetch_array($a)) {
    array_push($list, $arr);
  }
  if ($list) {
    $res["status"] = true;
    $res["mag"] = "请求成功";
    $res["total"] = $total;
    $res["data"] = $list;
  } else {
    $res["status"] = false;
    $res["mag"] = "数据库错误";
  }
} else {
  $res["status"] = false;
  $res["mag"] = "数据错误";
}
echo (json_encode($res, JSON_UNESCAPED_UNICODE));
